package xfsale.service.impl.j2ee;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import xfsale.exception.AppException;
import xfsale.packet.*;

public final class UserLogin extends BaseOperation {

	private UserLoginRequest request;
	private UserLoginResponse response;

	public UserLogin(UserLoginRequest request, UserLoginResponse response) {
		super(request, response);

		this.request = request;
		this.response = response;

		response.returnCode = "0000";
		response.returnMsg = "柜员登录成功";
	}

	//检查输入参数
    @Override
	protected void checkData() throws Exception {
		super.checkData();

		if (!request.opPassword.matches("^.{1,64}$"))
			throw new AppException("柜员登录密码不合法");
	}

	//功能处理
    @Override
	protected void process() throws Exception {
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			logger.debug("柜员登录...");
			stmt = conn.prepareStatement("" +
				"select * from OpAcct " +
				"where opCode = ? " +
				"and password = ? "
			);
			stmt.setString(1, request.opCode);
			stmt.setString(2, request.opPassword);
			rs = stmt.executeQuery();
			if (!rs.next()) {
				throw new AppException("柜员登录密码错误.");
			}

			response.opName = rs.getString("opName");

			logger.debug("柜员登录成功.");
		} catch (Exception e) {
			logger.error("柜员登录错误. " + e);
			throw e;
		} finally {
			stmt.close();
		}
	}

}